package com.csi.bumblebee.util;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加盐加密工具类
 *
 * @author HUOJT
 */
public class MD5Utils implements Serializable {

    private static final long serialVersionUID = 103L;

    /**
     * 将密码转换成字符串(MD5散列值)
     *
     * @param str 待加密内容
     * @return 加盐加密后的内容
     */
    public static String str2MD5(String str) {

        // 加盐
        str += "@JiaYan123#";

        byte[] digest = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            digest = md5.digest(str.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //16表示转换为16进制数
        String result = null;
        if (digest != null) {
            result = new BigInteger(1, digest).toString(16);
        }
        return result;
    }

}
